<?php
/**
 * Created by PhpStorm.
 * author :xupeng
 * Date: 2016/7/6
 * Time: 16:52
 */
namespace Home\Biz;

class PromotionBiz{

    /**
     * 查询产品数据
     * @param $promotion_name 促销名称
     * @param $pageSize 每页显示数量
     * @param $index 当前页码
     * @return result 查询结果集
     */
    public function querypromotion($promotion_name,$pageSize, $index)
    {
        if (!empty ($promotion_name)) {
            $condition_array['promotion_name'] = array('like', "%".$promotion_name."%");
        }
        $list_order_infos = M('promotion')->Field()->where($condition_array)->page($index, $pageSize)->select();
        // 总记录数
        $totalCount = M('promotion')->where($condition_array)->count();
        // 总页数
        $totalPage = self::caculateTotalPage($totalCount, $pageSize);
        // 当前页记录数
        $currentCount = $list_order_infos . $pageSize;
        // 下一页页码
        $nextIndex = $index + 1;
        // 上一页页码 如果是第一页，页码仍然为1
        $preIndex = 1;
        if ($index > 1) {
            $preIndex = $index - 1;
        }

        $resultMap = array(
            'totalCount' => $totalCount,
            'totalPage' => $totalPage,
            'currentPage' => $currentCount,
            'nextIndex' => $nextIndex,
            'preIndex' => $preIndex,
            'index' => $index,
            'datas' => $list_order_infos
        );
        return $resultMap;

    }

    /**
     * 添加产品促销信息
     * @param $promotion 产品促销信息
     */
    public static function addpromotion($promotion){
        M('promotion')->add($promotion);
    }

    /**
     * 修改产品促销信息
     * @param $promotion 产品促销信息
     */
    public static function updatepromotion($promotion){
        M('promotion')->where('promotion_id="%s"', array($promotion['promotion_id']))->save($promotion);
    }

    /**
     * 生成唯一主键
     * @return string
     */
    public static function generatepromotionId(){
        $seq = 10000000 + CommonBiz::getNewSeq('promotion_id');
        return 'PRO' . $seq;
    }

    public static function caculateTotalPage($totalCount, $pageSize){
        $count = intval($totalCount / $pageSize);
        $countLeft = $totalCount % $pageSize;
        if ($countLeft != 0) {
            $count = $count + 1;
        }
        return $count;
    }
   
}

?>